<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
    <title>开始游戏</title>

    <!-- Bootstrap -->
    <link href="bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://cdn.bootcss.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->


    <script src="js/jquery-3.2.1.min.js"></script>
    <script src="bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>

    <script type="text/javascript">
        //页面中统一规定，石头为0，剪刀为1，布为2
        var id = location.search.substr(4);

        function combatscore_add(){

            $.ajax({
                type: "GET",
                dataType : "json",
                url: "../api/game/combatscore/add?id="+id,
                success: function(msg){
                    //alert(msg.code);
                    console.info(msg.message);
                    initTitleDate();
                }
            });
        }

        function combatscore_update(code){

            $.ajax({
                type: "POST",
                contentType:"application/json",
                url: "../api/game/combatscore/update",
                data: '{"id":"'+id+'","code":"'+code+'","random":'+Math.random()+'}',

                success: function(msg){
                    //alert(msg.code);
                    console.info(msg.code);
                    initTitleDate();
                    $('a').removeAttr("disabled");
                }
            });
        }


        function initTitleDate() {
            $.ajax({
                type : "GET",
                url : "../api/game/combatscore/query",
                data : {
                    "id" : id,"random":Math.random()
                },
                dataType : "json",
                success : function(msg) {
                    //alert(msg.code);
                    console.info(msg.data);
                    var total = msg.data.total;
                    var win = msg.data.win;
                    $("#total").text(total);
                    $("#win").text(win);
                }
            });
            $.ajax({
                type : "GET",
                url : "../loginOnline.html?random="+Math.random(),
                dataType : "json",
                success : function(msg) {
                    //alert(msg.code);
                    var totalonline=msg.data;
                    $("#totalonline").text(totalonline);
                }
            });
        }

        function test(wj_num) {
            $('a').attr('disabled',"true");
            var round_num = Math.round(Math.random() * 2);
            console.info(round_num);
            if (wj_num === 0 && round_num === 0) {
                $("#img1").attr('src', "./img/gamepj_st.jpg");
                combatscore_update(2);
            } else if (wj_num === 0 && round_num === 1) {
                $("#img1").attr('src', "./img/gamewin_st.jpg");
                combatscore_update(0);
            } else if (wj_num === 0 && round_num === 2) {
                $("#img1").attr('src', "./img/gamelose_st.jpg");
                combatscore_update(1);
            } else if (wj_num === 1 && round_num === 0) {
                $("#img1").attr('src', "./img/gamelose_jd.jpg");
                combatscore_update(1);
            } else if (wj_num === 1 && round_num === 1) {
                $("#img1").attr('src', "./img/gamepj_jd.jpg");
                combatscore_update(2);
            } else if (wj_num === 1 && round_num === 2) {
                $("#img1").attr('src', "./img/gamewin_jd.jpg");
                combatscore_update(0);
            } else if (wj_num === 2 && round_num === 0) {
                $("#img1").attr('src', "./img/gamewin_b.jpg");
                combatscore_update(0);
            } else if (wj_num === 2 && round_num === 1) {
                $("#img1").attr('src', "./img/gamelose_b.jpg");
                combatscore_update(1);
            } else if (wj_num === 2 && round_num === 2) {
                $("#img1").attr('src', "./img/gamepj_b.jpg");
                combatscore_update(2);
            }

            initTitleDate();
        }


        combatscore_add();
    </script>
</head>
<body>
<div class="jumbotron text-center">
    <h1>石头剪刀布，人机争霸战!</h1>
    <em>在线人数统计：</em><em id="totalonline">xxx</em><br> <em>总局数：</em><em
        id="total">xxx</em><br> <em>赢局数：</em><em id="win">xxx</em><br>
    <p>
        <a class="btn btn-primary btn-lg" href="javascript:test(0)"
           role="button">石头</a> <a class="btn btn-primary btn-lg"
                                   href="javascript:test(1)" role="button">剪刀</a> <a
            class="btn btn-primary btn-lg" href="javascript:test(2)"
            role="button">布</a>
    </p>
    <img id="img1" src="./img/initgame.png" class="img-circle">
</div>


</body>
</html>